//
//  QSBaseTableViewController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/16.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSBaseTableViewController.h"
#import "QSRefreshHeader.h"
#import "QSRefreshFooter.h"
@interface QSBaseTableViewController ()

/** 页码 默认是1 */
@property(nonatomic, assign) NSInteger pageIndex;

/** 注册的cellIdentifier的集合 */
@property (nonatomic, strong) NSMutableSet *mutableSet;

@end

@implementation QSBaseTableViewController

- (instancetype)init {
    if (self = [super init]) {
        _pageIndex = 1;
        _dataArray = [NSMutableArray array];
        _style = UITableViewStylePlain;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
}

#pragma mark - **************** Public Methods
- (void)setRefreshHeaderTextColor:(UIColor *)textColor {
    QSRefreshHeader *mj_header = (QSRefreshHeader *)self.tableView.mj_header;
    mj_header.stateLabel.textColor = textColor;
}

- (void)setRefreshFooterTextColor:(UIColor *)textColor {
    QSRefreshFooter *mj_footer = (QSRefreshFooter *)self.tableView.mj_footer;
    mj_footer.stateLabel.textColor = textColor;
}

- (void)addRefreshHeader {
    WeakSelf(weakSelf);
    [self.tableView addRefreshHeaderWithRefreshingBlock:^{
        weakSelf.pageIndex = 1;
        [weakSelf tableViewShouldUpdateDataByPageIndex:weakSelf.pageIndex];
    }];
}

- (void)addRefreshFooter {
    WeakSelf(weakSelf);
    [self.tableView addRefreshFooterWithRefreshingBlock:^{
        weakSelf.pageIndex ++;
        [weakSelf tableViewShouldUpdateDataByPageIndex:weakSelf.pageIndex];
    }];
}

- (void)endRefreshing {
    [self.tableView qs_endRefreshing];
}

- (void)endRefreshingWithNoMoreData {
    [self.tableView qs_endRefreshingWithNoMoreData];
}

- (void)startRefreshing {
    [self startRefreshing:NO];
}

- (void)startRefreshing:(BOOL)animated {
    self.pageIndex = 1;
    if (animated) {
        [self.tableView qs_startRefreshing];
    } else {
        [self tableViewShouldUpdateDataByPageIndex:self.pageIndex];
    }
}

#pragma mark - **************** RefreshDataManager
- (void)tableViewShouldUpdateDataByPageIndex:(NSInteger)pageIndex {}

#pragma mark - ***************** QSMultiCellTableViewControllerProtocol
- (id<QSBaseCellItemDataProtocol>)multiCellTableViewController:(QSBaseTableViewController *)controller cellItemDataForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}
- (NSInteger)numberOfSectionsInMultiCellTableViewController:(QSBaseTableViewController *)controller {
    return 1;
}

- (NSInteger)multiCellTableViewController:(QSBaseTableViewController *)controller numberOfItemsInSection:(NSInteger)section {
    return 0;
}

- (BOOL)isShowCornerSection {
    return YES;
}

- (BOOL)isUseMultiCellTableView {
    return NO;
}

#pragma mark - **************** UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self isUseMultiCellTableView]) {
        NSInteger rows = [self multiCellTableViewController:self numberOfItemsInSection:section];
        return rows;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([self isUseMultiCellTableView]) {
        NSInteger sections = [self numberOfSectionsInMultiCellTableViewController:self];
        return sections;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self isUseMultiCellTableView]) {
        id<QSBaseCellItemDataProtocol> item = [self multiCellTableViewController:self cellItemDataForRowAtIndexPath:indexPath];
        
        Class registerClass = NSClassFromString(item.cellIdentifier);
        NSAssert(registerClass, @"必须需要设置cellIdentifier");
        
        if (![self.mutableSet containsObject:registerClass]) {
            [tableView registerClass:registerClass forCellReuseIdentifier:item.cellIdentifier];
            [self.mutableSet addObject:registerClass];
        }
        
        UITableViewCell<QSBaseTableViewCellProtocol> *cell = [tableView dequeueReusableCellWithIdentifier:item.cellIdentifier forIndexPath:indexPath];
        NSAssert(cell, @"没有找到cellIdentifier对应的Cell 继承QSBaseMultiTableViewCell");
        [cell configureCellWithItem:item];
        if ([self isShowCornerSection]
            && item.cellWidth
            && item.cellHeight) {
//            [cell addSectionCornerWithTableView:tableView
//                                      indexPath:indexPath
//                                cornerViewframe:CGRectMake(0, 0, item.cellWidth, item.cellHeight)
//                                   cornerRadius:8];
        }
        cell.separatorInset = item.cellSeapratorInset;
        return cell;
    }
    
    static NSString *cellIdentifier = @"cellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}

#pragma mark - **************** UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id<QSBaseCellItemDataProtocol> item = [self multiCellTableViewController:self cellItemDataForRowAtIndexPath:indexPath];
    if (item.cellHeight) {
        return item.cellHeight;
    }
    return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    id<QSBaseCellItemDataProtocol> item = [self multiCellTableViewController:self cellItemDataForRowAtIndexPath:indexPath];
    
    if (!item) {
        return;
    }
    
    if (item.cellHeight != 0) {
        return;
    }
    
    //如果没有设置高度则缓存自动计算的高度
    NSNumber *height = @(cell.frame.size.height);
    item.cellHeight = height.floatValue;
    
    //重新添加圆角
    if ([self isShowCornerSection]
        && item.cellWidth
        && item.cellHeight) {
//        [cell addSectionCornerWithTableView:tableView
//                                  indexPath:indexPath
//                            cornerViewframe:CGRectMake(0, 0, item.cellWidth, item.cellHeight)
//                               cornerRadius:8];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

#pragma mark - **************** Setter Getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavgationBarHeight) style:_style];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorColor = [UIColor QSColorGrayD8DFE4];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableSet *)mutableSet {
    if (!_mutableSet) {
        _mutableSet = [NSMutableSet set];
    }
    return _mutableSet;
}

@end
